package org.christopher.mybatisplusdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.christopher.mybatisplusdemo.common.UserCondition;
import org.christopher.mybatisplusdemo.entity.User;
import org.christopher.mybatisplusdemo.mapper.UserMapper;
import org.christopher.mybatisplusdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> selectUserAll() {
        return userMapper.selectList(null);
    }

    @Override
    public List<User> selectUserByIds(List<Integer> ids) {

        ids.add(1);
        ids.add(11);

        return userMapper.selectBatchIds(ids);
    }

    @Override
    public User selectUserById(Integer id) {
        return userMapper.selectById(id);
    }

    @Override
    public User loginUser(User user) {

        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>(user);

        User loginUser = userMapper.selectOne(userQueryWrapper);

        return loginUser;
    }

    @Override
    public int selectUserCount(User user) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>(user);


        return 0;//userMapper.selectCount(userQueryWrapper);
    }

    @Override
    public int addUser(User user) {

        return userMapper.insert(user);
    }

    @Override
    public int updataUser(User user) {

        int i = userMapper.updateById(user);

        return i;
    }

    @Override
    public int updateUser2(User user) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.set("name",user.getName());
        updateWrapper.ge("cash",user.getCash());
        return userMapper.update(user,null);
    }

    @Override
    public int deleteUser(Integer id) {
        return 0;
    }

    @Override
    public List<User> selectUserAllByCondition(UserCondition condition) {

        QueryWrapper<User> userQueryWrapper  = new QueryWrapper<>(new User()).like("name",condition.getName()).ge("cash",condition.getCash());
        return userMapper.selectList(userQueryWrapper);
    }
}
